package com.nextbusmn.server.JDOWrappers;

import java.util.List;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.nextbusmn.server.dataobjects.BusRouteData;
import com.nextbusmn.server.dataobjects.BusTripData;
import com.nextbusmn.server.dataobjects.NextBusData;
import com.nextbusmn.server.dataobjects.StopTimeData;

public class BusRouteJDOWrapper extends JDOWrapper {
	
	// creates a new wrapped BusRouteData object -- this might be slowing down the import???
	// 	(Simeon's example just had a static method, but I tried to move things into the 
	//   abstract JDOWrapper class...maybe that was not the best move?)
	public BusRouteJDOWrapper(BusRouteData data) {
		setData(data);
	}
	
	// check if a data object already exists in the datasore
	@Override
	public boolean dataExists(NextBusData data) {
		BusRouteData id = getByRouteID(((BusRouteData) data).getRouteID());
		return id != null;
	}
	
	// get a Bus Route Data object by primary key as a string (will throw error if object
	// not in datastore)
	public static BusRouteData getByID(String routeID) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		return (BusRouteData) pm.getObjectById(BusRouteData.class, routeID);
	}
	
	public static Set<BusTripData> getTripsByRouteId(int routeID) {
		BusRouteData route = getByRouteID(routeID);
		return route.getBusTripData();
	}
	
	// get a Bus Route Data Object by route ID, using a query.
	// returns null if not found, or if duplicate keys exist 
	public static BusRouteData getByRouteID(int routeID) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		Query query = pm.newQuery(BusRouteData.class);
		query.setFilter("routeID == rIDParam");
		query.declareParameters("int rIDParam");
		
		List<BusRouteData> result = null;
		List<BusRouteData> detached = null;
		
		try {
			result = (List<BusRouteData>) query.execute(routeID);
			for (BusRouteData d : result) {
				d.getBusTripData();
			}
			detached = (List<BusRouteData>) pm.detachCopyAll(result);
		} finally {
			query.closeAll();
		}
		
		if (detached.size() > 1) {
			// this should probably become an error msg eventually
			System.out.println("Data problem: Key not unique!"); 
		}
		
		return detached.size() == 1 ? detached.get(0) : null;
	}
	
	public static BusRouteData getByRouteIDForUpdates(int routeID) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		Query query = pm.newQuery(BusRouteData.class);
		query.setFilter("routeID == rIDParam");
		query.declareParameters("int rIDParam");
		
		List<BusRouteData> result = null;
		List<BusRouteData> detached = null;
		
		try {
			result = (List<BusRouteData>) query.execute(routeID);
			for (BusRouteData d : result) {
				d.getBusTripData();
			}
			detached = (List<BusRouteData>) pm.detachCopyAll(result);
		} finally {
			
			
		}
		
		if (detached.size() > 1) {
			// this should probably become an error msg eventually
			System.out.println("Data problem: Key not unique!"); 
		}
		
		return detached.size() == 1 ? detached.get(0) : null;
	}
	
	// return all rows...probably don't want to ever do this, so might get rid of these methods
	public static List<BusRouteData> getAllRows() {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		
		Query query = pm.newQuery(BusRouteData.class);
		
		List<BusRouteData> result = null;
		List<BusRouteData> detached = null;
		
		try {
			result = (List<BusRouteData>) query.execute();
			for (BusRouteData d : result) {
				d.getBusTripData();
			}
			detached = (List<BusRouteData>) pm.detachCopyAll(result);
		} finally {
			query.closeAll();
		}
		
		return detached;
		
//		return (List<BusRouteData>) JDOWrapper.getAllRows(BusRouteData.class);
	}
	
	public static void update(BusRouteData busRoute) {
		PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
		try {
            pm.makePersistent(busRoute);
        } finally {
            pm.close();
        }

	}
	
	public static List<BusTripData> getAllTripsForRoute(int routeID){
		BusRouteData rD = getByRouteID(routeID);
		
		return BusTripJDOWrapper.getBusTripDataByParentId(rD.getKey());
	}

	public static List<StopTimeData> getAllStopTimesForRoute(int routeID) {
		BusRouteData rD = getByRouteID(routeID);
		
		return StopTimeJDOWrapper.getBusStopTimesByParentId(rD.getKey());
	}

}
